<template>
  <el-dialog title="设备类型管理"
             top="125px"
             :visible.sync="manageType"
             @close="closeAllModel">
    <hr class="boundary">
    <el-table
      :data="brandData"
      height="270px"
      style="width: 90%">
      <el-table-column
        type="index"
        prop="id"
        label="编号"
        width="135px"
        :show-overflow-tooltip="true">
      </el-table-column>
      <el-table-column
        prop="name"
        label="类型">
      </el-table-column>
      <el-table-column
        label="操作"
        width="180px">
        <template slot-scope="scope">
          <el-button size="small" @click="showModifyType(scope.$index,scope.row.id)">修 改</el-button>
          <el-button size="small" type="danger" @click="showDelModel(scope.row.id)">删 除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <hr class="boundary">
    <add-Type :addType="addType" @showBrandsList="showBrandsList" @closeModel="closeModel"></add-Type>
    <modify-type :modifyType="modifyType" :modifyInfo="modifyInfo" @showBrandsList="showBrandsList"
                  @closeModel="closeModel"></modify-type>
    <del-type :delType="delType" :delId="delId" @showBrandsList="showBrandsList"
               @closeModel="closeModel"></del-type>
    <div slot="footer" class="dialog-footer">
      <el-button @click="closeAllModel">关 闭</el-button>
      <el-button type="success" @click="showAddType">添 加</el-button>
    </div>
  </el-dialog>
</template>

<script>
  import addType from './type/addType'
  import modifyType from './type/modifyType'
  import delType from './type/delType'

  export default {
    components: {
      addType,
      modifyType,
      delType
    },
    props: {
      manageType: Boolean
    },
    data () {
      return {
        addType: false,
        modifyInfo: {},
        modifyType: false,
        delId: 0,
        delType: false,
        brandData: []
      }
    },
    methods: {
      showBrandsList () {
        let self = this
        $.ajax({
          type: 'GET',
          url: this.api + 'types',
          headers:{
            'X-Requested-With': 'XMLHttpRequest'
          },
          xhrFields: {
            withCredentials: true
          },
          async: false,
          success (data) {
            self.brandData = data
          },
          error () {
            console.log('error')
          }
        })
      },

      showAddType () {
        this.addType = true
      },

      showModifyType (key, id) {
        this.modifyInfo = {
          'id': id, 'name': this.brandData[key].name
        }
        console.log(this.modifyInfo)
        this.modifyType = true
      },

      showDelModel (id) {
        this.delId = id
        console.log(this.delId)
        this.delType = true
      },

      closeModel () {
        this.addType = false
        this.modifyId = 0
        this.modifyType = false
        this.delId = 0
        this.delType = false
      },

      closeAllModel () {
        this.$emit('closeModel')
      }
    },
    mounted () {
        this.showBrandsList()
    }
  }
</script>

<style scoped>
  @import '../../assets/public/model.css';

  .el-dialog__wrapper >>> .el-dialog {
    width: 655px;
  }

  .el-table--fit {
    margin: 10px auto;
    border: 1px solid #BBBBBB;
  }

  .el-dialog__wrapper >>> .el-table th.is-leaf {
    text-align: center;
    font-size: 16px;
    padding: 7px;
    border-bottom: 1px solid #BBBBBB;
  }

  .el-dialog__wrapper >>> .el-table td {
    text-align: center;
    font-size: 16px;
    padding: 7px 0;
    border-bottom: 1px solid #BBBBBB;
    word-break: keep-all;
    white-space: nowrap;
  }

  .el-button--success {
    float: left;
  }

  .el-dialog__footer .el-button--default, .el-button--success {
    margin: 0;
    border-radius: 8px;
    font-size: 15px;
    padding: 10px 26px;
  }
</style>
